package cn.com.bluemoon.daps.model.service.impl;

import cn.com.bluemoon.daps.model.dto.ContrastTableInfo;
import cn.com.bluemoon.daps.model.dto.DapDataModelTableDto;
import cn.com.bluemoon.daps.model.entity.DapDataModelRecord;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.entity.DapDataModelVersion;
import cn.com.bluemoon.daps.model.mapper.DapDataModelVersionMapper;
import cn.com.bluemoon.daps.model.service.DapDataModelRecordService;
import cn.com.bluemoon.daps.model.service.DapDataModelVersionService;
import cn.com.bluemoon.daps.model.utils.contrast.VersionContrast;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DapDataModelVersionServiceImpl extends ServiceImpl<DapDataModelVersionMapper, DapDataModelVersion>
        implements DapDataModelVersionService {

    @Resource
    DapDataModelRecordService recordService;

    @Override
    public List<ContrastTableInfo> versionContrast(String tableId, String recordIdOne, String recordIdTwo) {

        // 查询版本记录
        if(Long.parseLong(recordIdOne)<Long.parseLong(recordIdTwo)){
            String newId = recordIdTwo;
            recordIdTwo = recordIdOne;
            recordIdOne = newId;
        }
        DapDataModelVersion newVersion = getBaseMapper().selectList(new LambdaQueryWrapper<DapDataModelVersion>()
                .eq(DapDataModelVersion::getModelTableId, tableId).eq(DapDataModelVersion::getRecordId, recordIdOne))
                .stream().findFirst().get();
        DapDataModelVersion oldVersion = getBaseMapper().selectList(new LambdaQueryWrapper<DapDataModelVersion>()
                .eq(DapDataModelVersion::getModelTableId, tableId).eq(DapDataModelVersion::getRecordId, recordIdTwo))
                .stream().findFirst().get();
        DapDataModelRecord newRecord = recordService.getBaseMapper().selectById(recordIdOne);
        DapDataModelRecord oldRecord = recordService.getBaseMapper().selectById(recordIdTwo);
        // 查询新版本记录
        List<ContrastTableInfo> list = Lists.newArrayList();
        if(newVersion != null && oldVersion != null){
            return VersionContrast.tableContrast(newVersion, oldVersion, newRecord, oldRecord);
        }else {
            return list;
        }
    }
}
